<?php
/*********
*    首页控制器   购物车
*/
namespace Home\Controller;
use Home\Controller\HomeController;
class GwcController extends HomeController {
 	public function _initialize(){
 		parent::_initialize();
 		
 	}
	/**
	 * 添加购物车
	 * 
	 */
 	public function addCart(){
 		$open_id = I('openid');
 		$goods_id = I('goods_id');
 		$price_attr_id = I('goods_attr_id');
 		$other_attr = I('name');
 		$num = I('num');
	    if($num==''){
	      $num=1;
	    }
	    if(empty($price_attr_id)){
	    	$dasq['goods_id']=$goods_id;
	    	$dasq['is_price_main']=1;
	    	$goods=M('goods_attr')->where($dasq)->find();
	    	if($goods['attr_kucun']<$num){
	    		$this->ajaxReturn(['status'=>-2,'msg'=>'库存不足','dfsa'=>2433]);
	    	}
	    }else{
	    	$dasq['goods_id']=$goods_id;
	    	$dasq['goods_attr_id']=$price_attr_id;
	    	$goods=M('goods_attr')->where($dasq)->find();
	    	if($goods['attr_kucun']<$num){
	    		$this->ajaxReturn(['status'=>-2,'msg'=>'库存不足','num'=>$goods]);
	    	}
	    }
    if(empty($price_attr_id)){
      $dasq['goods_id']=$goods_id;
      $dasq['is_price_main']=1;
      $goods=M('goods_attr')->where($dasq)->find();
        $cart=[
        'open_id'=>$open_id,
        'goods_id'=>$goods_id,
        'goods_attr_id'=>$goods['goods_attr_id'],
        'attr_names'=>$goods['attr_val'],
        'is_check'=>1,
        ];
        $nums=1;
    }else{
    	$dasq['goods_id']=$goods_id;
    	$dasq['goods_attr_id']=$price_attr_id;
    	$goods=M('goods_attr')->where($dasq)->find();
    	if($goods['attr_kucun']<$num){
    		$this->ajaxReturn(['status'=>-2,'msg'=>'库存不足']);
    	}
        $cart = [
        'open_id'=>$open_id,
        'goods_id'=>$goods_id,
        'goods_attr_id'=>$price_attr_id,
        'attr_names'=>$other_attr,
        'is_check'=>1,
      ];
      $nums=$num;
    }
 		
 		$cart_find = M('Cart')->where($cart)->find();
 		if($cart_find){
 			$r = M('Cart')->where($cart)->setInc('num',$nums);   
 		}else{
            $cart['num']=$nums;
 		    
 			$r = M('Cart')->add($cart);
 		}
		if(!$r){
			$this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙']);
		}
		$this->ajaxReturn(['status'=>1,'data'=>$r,'msg'=>'请求成功']);
 	}
 	/**
 	 * 获取购物车信息
 	 */
 	public function getCartByOpenId(){
 		$open_id = I('openid');
 		//$cart = M('Cart')->where(array('open_id'=>$open_id))->save(array('is_check'=>0));
 		$cart = M('Cart')->where(array('open_id'=>$open_id))->select();
 		$personJifen=M('member')->where("openid='{$open_id}'")->find();
 		$countjifen=0;
 		 if($cart){
 			foreach ($cart as $k=>$v){
 				//dump($v['goods_id']);
 				$shangpin = M('Goods')->where(array('goods_id'=>$v['goods_id']))->find();
 				//dump($shangpin['image']);
 				$price = M('Goods_attr')->where(array('goods_attr_id'=>$v['goods_attr_id']))->find();
 				$attr_price = $price['price'];
 				$cartInfo[$k]['cart_id'] = $v['cart_id'];
 				$cartInfo[$k]['goodsName'] = $shangpin['name'];
 				$cartInfo[$k]['attr_fan'] = $price['fanjifen'];
 				$cartInfo[$k]['goodsImg'] = $shangpin['image'];
 				$cartInfo[$k]['num'] = intval($v['num']);
 				$cartInfo[$k]['is_check'] = $v['is_check'];
 				$cartInfo[$k]['price'] = $attr_price;
 				$cartInfo[$k]['attr_kucun'] = (int)$price['attr_kucun'];
 				$cartInfo[$k]['shuxing'] = $v['attr_names'];
 				$cartInfo[$k]['jifen'] = $price['jifen'];
 				$cartInfo[$k]['goods_id'] = $price['goods_id'];
 				$cart = M('Cart')->where('cart_id ='.$v['cart_id'])->find();
 				$kucun = M('Goods_attr')->where('goods_attr_id ='.$cart['goods_attr_id'])->find();
 				if($kucun['attr_kucun'] < $cart['num']){
 				    $cartInfo[$k]['kucun_num'] = 1;
 				    if($kucun['attr_kucun']<=0){
 				    	$cart = M('Cart')->where('cart_id ='.$v['cart_id'])->delete();
 				    }
 				    $cartInfo[$k]['num'] = $kucun['attr_kucun'];
 				}
 				if($v['is_check']=='1'){
 					$countjifen=$countjifen+$cartInfo[$k]['jifen']*$cartInfo[$k]['num']; 
 				}
 				$cart = M('Cart')->where('cart_id ='.$v['cart_id'])->save(array('num'=>$cartInfo[$k]['num']));
 			}
 		}
    if($cartInfo==Null){
      $cartInfo=0;
    } 
        $cat['info']= $cartInfo;
 		$cat['zongjifen']=$personJifen['jifen'];//用户可用积分
 		$cat['countjifen']=$countjifen;//合计购买的积分
 	    //dump($cat);die;
 		$this->ajaxReturn($cat);
 	} 
 	/**
 	 * 修改购物车数量
 	 */
 	public function changeCartNum(){
 		$num = I('num');
 		$open_id = I('openid');
 		$cart_id = I('cart_id');
 		//dump($num);die;
 		$cart = M('Cart')->where('cart_id ='.$cart_id)->find();
 		$kucun = M('Goods_attr')->where('goods_attr_id ='.$cart['goods_attr_id'])->find();
 		if($kucun['attr_kucun'] < $num){
 		    $this->ajaxReturn(['status'=>-3,'msg'=>'商品库存不足']);
 		}
 		$r = M('Cart')->where(array('cart_id'=>$cart_id))->setField('num',$num);
 		if(!$r){
 			$this->ajaxReturn(['status'=>-1,'msg'=>'数量没有变化']);
 		}else{
 		    $this->ajaxReturn(['status'=>1,'msg'=>'请求成功']);
 		}
 		
 	}
 	/**
 	 * 删除购物车
 	 */
 	public function delCart(){
 	    $open_id = I('openid');
 	    $cart_id = I('cart_id');
 	    $arr['cart_id']=$cart_id;
 	    $arr['open_id']=$open_id;
 	    $r = M('Cart')->where($arr)->delete();
 	    if(!$r){
 	    	//$this->ajaxReturn($arr);die;
 	     	$this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙']);
 	    }
 	      $this->ajaxReturn(['status'=>1,'msg'=>'请求成功']);
 	}
 	
 	/**
 	 * 选中购物车   
 	*/
 	public function checkCart(){
 	    $open_id = I('openid');
 	    $cart_id = I('cart_id');
 	    $is_check = I('is_check');
 	    $cart = M('Cart')->where('cart_id ='.$cart_id)->find();
            $goods_attr = M('Goods_attr')->where(array('goods_attr_id'=>$cart['goods_attr_id']))->find();
           // if(!empty($goods_attr['price'])){
             // 	$status = 1;
            //}
            $carts = M('Cart')->where(array('open_id'=>$open_id,'is_check'=>1,'cart_id'=>array('neq',$cart_id)))->select();
            if($carts){
                   foreach ($carts as $v){
                      $goods_attrs = M('Goods_attr')->where(array('goods_attr_id'=>$v['goods_attr_id']))->find();
                      if(!empty($goods_attr['price'])&&!empty($goods_attrs['jifen'])){
                          $this->ajaxReturn(['status'=>-2,'msg'=>'积分商品和现金商品不能同时购买'],'JSONP');
                      }
                       if(!empty($goods_attr['jifen'])&&!empty($goods_attrs['price'])){
                          $this->ajaxReturn(['status'=>-2,'msg'=>'积分商品和现金商品不能同时购买'],'JSONP');
                      }
                  }
            }
           
 	    $kucun = M('Goods_attr')->where('goods_attr_id ='.$cart['goods_attr_id'])->find();
 	    if($kucun['attr_kucun'] < $cart['num']){
 	        $this->ajaxReturn(['status'=>-2,'msg'=>'所选商品库存不足'],'JSONP');
 	    }
 	    $r = M('Cart')->where(array('cart_id'=>$cart_id))->setField('is_check',$is_check);
 	   $cart=M('Cart')->where(array('open_id'=>$open_id,'is_check'=>1))->select();
 	    foreach($cart as &$vv){
 	    	$att=M('Goods_attr')->where('goods_attr_id ='.$vv['goods_attr_id'])->find();
 	    	$zongjifen=$vv['num']*$att['jifen'];
 	    	$countjifen+=$zongjifen;
 	    }
 	    if($countjifen==''){
 	    	$countjifen=0;
 	    }
 	    if(!$r){
 	        $this->ajaxReturn(['status'=>-1,'msg'=>'服务器繁忙']);
 	    }else{
 	        $this->ajaxReturn(['status'=>1,'msg'=>'请求成功','countjifen'=>$countjifen]);
 	    }
 	    
 	}
 	/**
 	 * 选中购物车(全选)
 	*/
 	public function checkCartAll(){
 	    $open_id = I('openid');
 	    $is_check = I('is_check');
 	    $cart = M('Cart')->where(array('open_id'=>$open_id))->select();
            $price = [];
            $jifen = [];
 	    foreach($cart as &$v){
 	        $kucun = M('Goods_attr')->where('goods_attr_id ='.$v['goods_attr_id'])->find();
 	        $cart_n = M('Cart')->where('cart_id ='.$v['cart_id'])->find();
 	        if($kucun['attr_kucun'] < $cart_n['num']){
 	            $kucun_num[] = 1;
 	        }
                if(!empty($kucun['jifen'])){
                  	$jifen[] = $kucun['jifen'];
                }
                if(!empty($kucun['price'])){
                  	$price[] = $kucun['price'];
                }
 	    }
            if(!empty($jifen)&&!empty($price)&&(count($jifen)+count('price')!=1)){
              	$this->ajaxReturn(['status'=>-2,'msg'=>'积分商品和现金商品不能同时购买']);
            }
 	    if($is_check == 1){
 	        if(in_array(1,$kucun_num)){
 	            $this->ajaxReturn(['status'=>-2,'msg'=>'购物车内商品库存不足']);
 	        }
 	    }
 	    
 	    $r = M('Cart')->where(array('open_id'=>$open_id))->setField('is_check',$is_check);
 	    $cart = M('Cart')->where(array('open_id'=>$open_id,'is_check'=>1))->select();
 	    $countjifen=0;
 	    foreach($cart as &$vv){
 	    	$att=M('Goods_attr')->where('goods_attr_id ='.$vv['goods_attr_id'])->find();
 	    	$zongjifen=$vv['num']*$att['jifen'];
 	    	$countjifen+=$zongjifen;
 	    }
 	    if($countjifen==''){
 	    	$countjifen=0;
 	    }
 	    $this->ajaxReturn(['status'=>1,'msg'=>'请求成功','countjifen'=>$countjifen]);
 	}
}
